package com.itic.appbase.framework.utils.qcode;

import java.io.File;
import java.util.Hashtable;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.itic.appbase.applications.files.utils.FileHelper;
import com.itic.appbase.framework.utils.IdGen;
import com.itic.appbase.framework.utils.configuration.PropertyHelper;

public class QcodeUtils {
	/**
	 * @param text 二维码详情
	 * @param width 二维码的宽
	 * @param height 二维码高
	 * @param format 图片格式
	 * @throws Exception
	 */
	public static String create(String text, int width, int height, String format) throws Exception {
        Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
        
        String rootDir = FileHelper.getRootDir();// 获取文件根路径
        String fileName = IdGen.uuid()+"."+format;
        
        File localFile = new File(rootDir + fileName);//获取保存文件的全路径
        if (!localFile.getParentFile().exists()) {//判断父文件夹是否存在
            localFile.getParentFile().mkdirs();
        }
        MatrixToImageWriter.writeToFile(bitMatrix, format, localFile);
        
        String downloadPath = PropertyHelper.getString("app.download.path");
        String projectName = PropertyHelper.getString("app.code");
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(downloadPath).append(projectName == null ? "" : (projectName+"_")).append("fileupload").append("/").append(fileName);
       
        return stringBuffer.toString();
        
    }
	
	
}
